<?php
/**
 * Created by PhpStorm.
 * User: pf
 * Date: 2017/9/10
 * Time: 12:07
 */
namespace app\admin\controller;

use app\admin\controller\CommonController;
use app\admin\model\Permission;
use app\admin\model\Role;
use app\admin\model\RolePermission;
use app\util\Util;
use app\admin\model\Admin;

class RoleController extends CommonController {
    public function index(){//角色列表
        $list = Role::field('id, name')
            ->paginate(10);

        return view('index', ['list' => $list]);

}
    /*
  * 添加
  */
    public function add()
    {
        return view('add');
    }

    /*
    * 添加
    */
    public function postInsert()
    {
        $all = input('post.');

        $username = $all['name'];


        $exist = Role::where('name', '=', $username)->find();
        if (!is_null($exist)) {
            return Util::ajaxReturn(0, '名称已存在', null);
        }

        $salt = Util::generateSalt();


        $data = [
            'name' => $username,

        ];


        $result = Role::insert($data);
        if ($result) {
            return Util::ajaxReturn(1, '添加成功', null);
        } else {
            return Util::ajaxReturn(0, '添加失败', null);
        }
    }

    /*
    * 编辑
    */
    public function edit()
    {
        $id = input('param.id');

        $role = Role::where('id', '=', $id)->field('id, name')->find();
        return view('edit', ['admin' => $role]);
    }

    /*
    * 修改
    */
    public function postSave()
    {
        $all = input('post.');

        $id = $all['id'];
        $username = $all['name'];


        $exist = Role::where('name', '=', $username)->where('id', '<>', $id)->find();
        if (!is_null($exist)) {
            return Util::ajaxReturn(0, '角色已存在', null);
        }

        $data = [
            'name' => $username,
        ];



        $result = Role::where('id', '=', $id)->update($data);
        if ($result) {
            return Util::ajaxReturn(1, '修改成功', null);
        } else {
            return Util::ajaxReturn(0, '修改失败', null);
        }
    }

    /*
    * 删除
    */
    public function del()
    {
        $id = input('get.id');

        $result = Role::where('id', '=', $id)->delete();
        if ($result) {
            return Util::ajaxReturn(1, '删除成功', null);
        } else {
            return Util::ajaxReturn(0, '删除失败', null);
        }
    }
    /**
     * 编辑权限
     */
    public function edit_role(){
         $id = input('param.id');
         $permissionList=Permission::all();
         $permissionList=$this->list_to_tree($permissionList);
         $userPermission=Role::find($id);
         $permission1=json_decode($userPermission->permission);
           // dump($permissionList);
        return view('edit_permission',compact('permissionList','id','permission1'));
    }
    public function savePermission(){
        $all=input('post.');
        $id=$all['id'];
        $data['permission']=json_encode($all['permission']);
        $role=Role::where('id', '=', $id)->update($data);
        $this->success('修改成功');

    }
    public function list_to_tree($list, $child=array(), $parent_id = 0)
    {
        $child = array();
        if (!empty($list)) {
            foreach ($list as $k => &$v) {
                if ($v['parent_id'] == $parent_id) {
                    $v['child'] = $this->list_to_tree($list, $child, $v['id']);
                    $child[] = $v;
                    unset($this->list_to_tree[$k]);
                }
            }
        }
        return $child;
    }


}